package com.itchu.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itchu.common.domain.PageResult;
import com.itchu.system.domain.SysRole;
import com.itchu.system.mapper.AuthorityMapper;
import com.itchu.system.mapper.SysRoleMapper;
import com.itchu.system.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class SysRoleServiceImpl implements SysRoleService {


    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private AuthorityMapper authorityMapper;

    /**
     * 查找角色的分页信息
     * @param key   搜索条件
     * @param page  当前页
     * @param rows  页大小
     * @param sortBy
     * @param desc
     * @return
     */
    @Override
    public PageResult<SysRole> findByPage(String key, Integer page, Integer rows, String sortBy, Boolean desc) {
        //开启分页
        PageHelper.startPage(page,rows);
        //创建一个封装条件的对象
        Example example = new Example(SysRole.class);
        if (!StringUtils.isEmpty(key)) {
            Example.Criteria criteria = example.createCriteria();
            criteria.orLike("name","%" + key + "%");
            criteria.orLike("id","%" + key + "%");
        }
        if (!StringUtils.isEmpty(sortBy)) {
            example.setOrderByClause(sortBy+" "+(desc ? "desc" : "asc"));
        }
        List<SysRole> sysRoles = sysRoleMapper.selectByExample(example);
        PageInfo<SysRole> pageInfo = new PageInfo<>(sysRoles);
        return new PageResult<>(pageInfo.getTotal(),pageInfo.getPages(),sysRoles);
    }

    /**
     * 保存角色信息
     * @param sysRole
     * @return
     */
    @Override
    public void addSysRole(SysRole sysRole) {
        if (sysRole != null) {
            sysRole.setCreateTime(new Date());
            sysRoleMapper.insertSelective(sysRole);
        }
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @Override
    public void deletSysRole(Long id) {
        sysRoleMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改角色
     * @param sysRole
     * @return
     */
    @Override
    public void updateSysRole(SysRole sysRole) {
        if (sysRole != null) {
            sysRole.setUpdateTime(new Date());
            sysRoleMapper.updateByPrimaryKey(sysRole);
        }
    }

    /**
     * 根据角色id查找角色
     * @param id 角色id
     * @return
     */
    @Override
    public SysRole findById(Long id) {
        SysRole sysRole = sysRoleMapper.selectByPrimaryKey(id);
        return sysRole;
    }

    /**
     * 查询出所有角色
     * @return
     */
    @Override
    public List<SysRole> AllSysRole() {
        List<SysRole> sysRoles = sysRoleMapper.selectAll();
        return sysRoles;
    }

    /**
     * 更新角色的权限
     * @param rid  角色id
     * @param ids  权限ids
     * @return
     */
    @Override
    public void updateRoleAuthorities(Long rid, List<Long> ids) {

        // 前端的树形组件如果一个父菜单下有多个子菜单，我只选中一个子菜单，他的父id是传不进来的，所以要查出子菜单的父id
        // 先删后增
        authorityMapper.deleteRoleAuthorities(rid);
        authorityMapper.addRoleAuthorities(rid,ids);
    }
}
